"""
中信接口测试
"""
import os
import sys
import django

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()

from core.zhongxin.client import ZXClient
from core.zhongxin.models import OrderRecord, OrderRefund
from apps.supply_order.models import RefundRecord
from apps.czb.models import Orders


def refund():
    out_trade_no = "20072416503302298713"
    refundAmount = "4.06"
    orders_instance = Orders.objects.get(out_trade_no=out_trade_no)
    zx_order_record = OrderRecord.objects.get_order_record_instance(out_trade_no)
    oldOriginSerialNo = zx_order_record.out_trade_no
    oldOriginTranTime = zx_order_record.requestTimestamp.strftime("%Y-%m-%d %H:%M:%S")
    installment = zx_order_record.installment
    cashAmount = str(zx_order_record.totalAmount)
    zx_refund_instance = OrderRefund.objects.create_refund(oldOriginSerialNo, oldOriginTranTime, cashAmount,
                                                           installment)
    out_refund_no = zx_refund_instance.out_refund_no
    refund_instance = RefundRecord.objects.create_refund(
        refund_fee=refundAmount,
        refund_type=orders_instance.pay_type,
        out_refund_no=out_refund_no
    )
    orders_instance.refund = refund_instance
    orders_instance.result = 2
    orders_instance.save()
    content = {"code": 200, "message": "OK", "result": {}}
    print(content, "车主邦退款异步通知，返回数据")


if __name__ == '__main__':
    refund()
